package com.du.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * @author djg
 * @date 2020/11/26
 * @des 切面类
 *
 * @Aspect： 告诉Spring当前类是一个切面类
 */

@Aspect
public class LogAspects {

    @Pointcut("execution(public int com.du.aop.MathCalculator.*(..))")
    public void pointCut() {

    }

    //@Before在目标方法执行之前切入，切入表达式（指定在哪个方法切入）
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        System.out.println(joinPoint.getSignature().getName() + "运行。。。。@Before参数列表是：{"+ Arrays.toString(args) +"}");
    }

    @After("pointCut()")
    public void logEnd(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName()+"正常结束。。。。@After");
    }

    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturn(JoinPoint joinPoint,Object result) {
        System.out.println(joinPoint.getSignature().getName() + "正常返回。。。。@AfterReturning运行结果是：{" + result + "}");
    }

    //JoinPoint 一定要出现在参数表的第一位
    @AfterThrowing(value = "pointCut()",throwing = "e")
    public void logException(JoinPoint joinPoint,Exception e) {
        System.out.println(joinPoint.getSignature().getName() + "除法异常。。。。@AfterThrowing异常信息是：{" + e + "}");
    }
}
